package com.example.networklab.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissionConfig {
    @Value("${spring.redis.host}")
    private String serverAddress;
    @Value("${spring.redis.port}")
    private String port;
    @Value("${spring.redis.password:#}")
    private String password;

    @Bean
    public RedissonClient redissionClient() {
        //配置
        Config config = new Config();
        if (password.equals("#")) {
            config.useSingleServer().setAddress(getRedisUrl());
        } else {
            config.useSingleServer().setAddress(getRedisUrl()).setPassword(password);
        }
        //创建RedissonClient对象
        return Redisson.create(config);
    }

    private String getRedisUrl() {
        return "redis://" + serverAddress + ":" + port;
    }
}
